class CreateAvailabilities < ActiveRecord::Migration
  def self.up
    create_table :availabilities do |t|
      t.integer :user_id
      t.integer :event_id
    end
    add_index :availabilities, :event_id
    add_index :availabilities, :user_id
    
    Availability.reset_column_information
    
    Attendance.find(:all, :conditions => ["state='cfw'"]).each do |attendance|
      Availability.create(:event_id => attendance.event_id, :user_id => attendance.user_id)
      attendance.destroy
    end
    remove_column :attendances, :state
  end

  def self.down
    add_column :attendances, :state, :string
    
    Attendance.reset_column_information
    Attendance.update_all("state='assigned'")
    
    Availability.find(:all).each do |availability|
      Attendance.create(:event_id => availability.event_id, :user_id => availability.event_id, :state => 'cfw')
    end
    
    drop_table :availabilities
  end
end
